﻿using System;
using Locomotion;
using UnityEngine;

namespace Combat
{
    public class Hitter : MonoBehaviour
    {
        public Vector2 MoveDirection => _mover.MoveDirection;
        
        [SerializeField] private int damage = 1;

        private Mover _mover;
        private Health _health;


        private void Awake()
        {
            _mover = GetComponent<Mover>();
            _health = GetComponent<Health>();
        }

        private void OnTriggerEnter2D(Collider2D other)
        {
            if (other.TryGetComponent(out Health health))
            {
                health.TakeDamage(damage, _mover.MoveDirection);
                Explode();
            }
        }

        private void Explode()
        {
            _health.Die();
        }
    }
}